package com.atguigu.service;

import com.atguigu.dao.FileDao;
import com.atguigu.entity.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FileServiceImpl implements FileService{

    @Autowired
    private FileDao fileDao;

    @Override
    public List<File> getFileByUserId(Integer userId) {
        return fileDao.getFileByUserId(userId);
    }

    @Override
    public int save(File newFile) {
        //newFile.setIsImg();
        int isImg = newFile.getType().startsWith("image")? 1 : 0;
        newFile.setIsImg(isImg);
        newFile.setDownloadCount(0);
        newFile.setUploadTime(new Date());
        return fileDao.save(newFile);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public File findById(Integer id) {
        return fileDao.findById(id);
    }

    @Override
    public int update(File downloadFile) {
        return fileDao.update(downloadFile);
    }

    @Override
    public int delete(Integer id) {
        return fileDao.delete(id);
    }
}
